package cn.hd01.export;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.util.StringUtils;

import cn.hd01.common.BaseConstant.IsBalance;
import cn.hd01.export.listener.RowProccessListener;
import cn.hd01.export.result.ResultListener;
import cn.hd01.repository.entity.COrder;
import cn.hd01.service.COrderService;
import cn.hd01.service.DistributorLevelService;
import cn.hd01.sms.SmsService;

public class COrderRowProcess extends AbstractRowProccess<COrder>{
	
	private COrderService cOrderService;
	
	private DistributorLevelService distributorLevelService;
	
	private SmsService smsService;
	
	private COrder order;
	
	List<COrder> list = new ArrayList<COrder>();
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	
	public COrderRowProcess(RowProccessListener listener, COrderService cOrderService, DistributorLevelService distributorLevelService, SmsService smsService) {
		super(listener);
		this.cOrderService = cOrderService;
		this.distributorLevelService = distributorLevelService;
		this.smsService = smsService;
	}

	@Override
	public COrder mapper(String[] row) {
		COrder order = new COrder();
		order.setCustomerPhoneNo(StringUtils.trimAllWhitespace(row[0]));
		order.setDistributorPhoneNo(StringUtils.trimAllWhitespace(row[1]));
		order.setProjectName(StringUtils.trimAllWhitespace(row[2]));
		try{
			order.setAmount(new BigDecimal(StringUtils.trimAllWhitespace(row[3])));
		}catch(Exception e){
			order.setAmount(null);
		}
		
		return order;
	}

	@Override
	public void process(COrder order) {
		ResultListener listener = (ResultListener) this.listener;
		cOrderService.fillCOrder(order, listener.getResult(), distributorLevelService.findAll());
		cOrderService.save(order);
		list.add(order);
	}
	
	@Override
	public String[] getTitle() {
		String[] arr = new String[13];
		arr[0] = "客户姓名";
		arr[1] = "客户手机号码";
		arr[2] = "消费金额";
		arr[3] = "渠道商姓名";
		arr[4] = "渠道商电话号码";
		arr[5] = "渠道商提成率";
		arr[6] = "渠道商提成";
		arr[7] = "服务老师姓名";
		arr[8] = "服务老师提成率";
		arr[9] = "服务老师提成";
		arr[10] = "是否结算";
		arr[11] = "消费项目";
		arr[12] = "创建时间";
		return arr;
	}
	
	@Override
	public String[] mapper(COrder obj) {
		String[] arr = new String[13];
		arr[0] = getIFNull(obj.getCustomerName(), "");
		arr[1] = getIFNull(obj.getCustomerPhoneNo(), "");
		arr[2] = getIFNull(obj.getAmount(), "0");
		arr[3] = getIFNull(obj.getDistributorName(), "");
		arr[4] = getIFNull(obj.getDistributorPhoneNo(), "");
		arr[5] = getIFNull(obj.getDistributorRebateRate() + "%", "0");
		arr[6] = getIFNull(obj.getDistributorRebate(), "0");
		arr[7] = getIFNull(obj.getServantName(), "");
		arr[8] = getIFNull(obj.getServantRebateRate() + "%", "0");
		arr[9] = getIFNull(obj.getServantRebate(), "0");
		arr[10] = getIFNull(IsBalance.getText(obj.getIsBalance()), "未知");
		arr[11] = getIFNull(obj.getProjectName(), "");
		arr[12] = getIFNull(sdf.format(obj.getCreateTime()), "");
		return arr;
	}
	
	@Override
	public List<COrder> find() {
		return cOrderService.findAll(order);
	}
	
	public COrder getOrder() {
		return order;
	}

	public void setOrder(COrder order) {
		this.order = order;
	}

	@Override
	public String[] reverseProcess(COrder obj) {
		return mapper(obj);
	}

	private String getIFNull(Object val, String defaultValue){
		return val == null ? defaultValue : String.valueOf(val);
	}

	@Override
	public void afterImport() {
		for(COrder order : list){
			smsService.sendFixTimeMsg(order.getDistributorPhoneNo(), new StringBuffer(order.getDistributorName()).append(",").append("，您的订单：客户为").append(order.getCustomerName()).append("，消费项目为").append(order.getProjectName()).append("，金额为").append(order.getAmount()).append("千郡后台已经录单。").toString());
		}
	}

}
